package Year22_Month03.day06;

import java.util.HashMap;
import java.util.Map;

public class Main3 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,2,2,2,5,4,2};
        System.out.println(MoreThanHalfNum_Solution(arr));
        
    }

    public static int MoreThanHalfNum_Solution(int [] array) {
        if(array.length == 1){
            return array[0];
        }
        Map<Integer,Integer> table = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            if(table.containsKey(array[i]) == false){
                table.put(array[i],0);
                continue;
            }
            table.put(array[i],table.get(array[i]) + 1);
            if(table.get(array[i]) >= array.length/2){
                return array[i];
            }
        }
        return -1;
    }
}
